package com.ruoyi.system.controller;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.ProvideRecords;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.InvoiceDetail;
import com.ruoyi.system.service.IInvoiceDetailService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 开票详情记录Controller
 * 
 * @author ruoyi
 * @date 2024-06-19
 */
@RestController
@RequestMapping("/system/detail")
public class InvoiceDetailController extends BaseController
{
    @Autowired
    private IInvoiceDetailService invoiceDetailService;

    /**detail
     * 查询开票详情记录列表
     */
    @GetMapping("/list")
    public AjaxResult list(@RequestParam("pageNum")Integer pageNum,
                           @RequestParam("pageSize")Integer pageSize,
                           HttpServletResponse response,
                           InvoiceDetail invoiceDetail)
    {
        List<InvoiceDetail> list = invoiceDetailService.selectInvoiceDetailList(invoiceDetail);

        // 对列表进行分页处理
        int total = list.size();
        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize, total);
        List<InvoiceDetail> paginatedList = list.subList(startIndex, endIndex);

        // 设置响应头
        DecimalFormat df = new DecimalFormat("0.00##");
        Double sumMoney = 0.0;
        //判断有没有选择开票类型
        if(invoiceDetail.getProvideType()!=null&&(invoiceDetail.getProvideType().equals("开票")||invoiceDetail.getProvideType().equals("作废"))){
            if(invoiceDetail.getProvideType().equals("开票")){
                response.setHeader("pCount", String.valueOf(list.size()));
                response.setHeader("zCount", "0");
                sumMoney = Math.abs(invoiceDetailService.getSumMoney(invoiceDetail));
            }else if(invoiceDetail.getProvideType().equals("作废")){
                response.setHeader("pCount", "0");
                response.setHeader("zCount", String.valueOf(list.size()));
                sumMoney = sumMoney -  invoiceDetailService.getSumMoney(invoiceDetail);
            }
            response.setHeader("sumMoney", df.format(sumMoney));
        }else{
            //如果没有选择开票类型
            invoiceDetail.setProvideType("开票");
            List<InvoiceDetail> pInvoiceDetails = invoiceDetailService.selectInvoiceDetailList(invoiceDetail);
            if(invoiceDetailService.getSumMoney(invoiceDetail)!=null){
                sumMoney = Math.abs(invoiceDetailService.getSumMoney(invoiceDetail));
            }
            response.setHeader("pCount", String.valueOf(pInvoiceDetails.size()));
            invoiceDetail.setProvideType("作废");
            List<InvoiceDetail> zInvoiceDetails = invoiceDetailService.selectInvoiceDetailList(invoiceDetail);
            if(invoiceDetailService.getSumMoney(invoiceDetail)!=null){
                sumMoney = sumMoney - Math.abs(invoiceDetailService.getSumMoney(invoiceDetail));
            }
            response.setHeader("zCount", String.valueOf(zInvoiceDetails.size()));
            response.setHeader("sumMoney",String.valueOf(sumMoney));
        }

        // 返回分页结果
        Map<String, Object> result = new HashMap<>();
        result.put("list", paginatedList);
        result.put("total", total);
        result.put("pageNum", pageNum);
        result.put("pageSize", pageSize);
        return AjaxResult.success(result);
    }

    /**
     * 查询开票详情记录列表
     */
    @GetMapping("/selectList")
    public AjaxResult selectList(@RequestParam("pageNum")Integer pageNum,
                           @RequestParam("pageSize")Integer pageSize,
                           InvoiceDetail invoiceDetail)
    {
        List<InvoiceDetail> list = invoiceDetailService.selectList(invoiceDetail);
        // 对列表进行分页处理
        int total = list.size();
        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize, total);
        List<InvoiceDetail> paginatedList = list.subList(startIndex, endIndex);
        // 返回分页结果
        Map<String, Object> result = new HashMap<>();
        result.put("list", paginatedList);
        result.put("total", total);
        result.put("pageNum", pageNum);
        result.put("pageSize", pageSize);
        return AjaxResult.success(result);
    }

    /**
     * 导出开票详情记录列表
     */
    @Log(title = "开票详情记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InvoiceDetail invoiceDetail)
    {
        List<InvoiceDetail> list = invoiceDetailService.selectInvoiceDetailList(invoiceDetail);
        ExcelUtil<InvoiceDetail> util = new ExcelUtil<InvoiceDetail>(InvoiceDetail.class);
        util.exportExcel(response, list, "开票详情记录数据");
    }

    /**
     * 获取开票详情记录详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(invoiceDetailService.selectInvoiceDetailById(id));
    }

    /**
     * 新增开票详情记录
     */
    @Log(title = "开票详情记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InvoiceDetail invoiceDetail)
    {
        return toAjax(invoiceDetailService.insertInvoiceDetail(invoiceDetail));
    }

    /**
     * 修改开票详情记录
     */
    @Log(title = "开票详情记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InvoiceDetail invoiceDetail)
    {
        return toAjax(invoiceDetailService.updateInvoiceDetail(invoiceDetail));
    }

    /**
     * 删除开票详情记录
     */
    @Log(title = "开票详情记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(invoiceDetailService.deleteInvoiceDetailByIds(ids));
    }
}
